package com.qhs.framework.utils;

import javax.print.*;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.standard.MediaPrintableArea;
import javax.print.attribute.standard.OrientationRequested;
import javax.print.attribute.standard.PrintQuality;
import javax.swing.*;
import java.io.*;

/**
 * Created by zano on 2017/6/15.
 */
public class PrintUtil {

    public static void init(File file){
        JFileChooser jFileChooser = new JFileChooser();//创建打印作业
         // File file = new File(ConfigUtil.get("system.path"));//获取选择的文件
        //构建打印请求属性集
        HashPrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
        pras.add(OrientationRequested.PORTRAIT);
        pras.add(PrintQuality.HIGH);
        //定位默认的打印服务
        PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
        InputStream fis =null;
        DocFlavor dof = null;
        String fileName = file.getName();
        if (fileName.endsWith(".gif")) {
            dof = DocFlavor.INPUT_STREAM.GIF;
        } else if (fileName.endsWith(".jpg")) {
            dof = DocFlavor.INPUT_STREAM.JPEG;
        } else if (fileName.endsWith(".png")) {
            dof = DocFlavor.INPUT_STREAM.PNG;
        }
        try {
             DocPrintJob job =  defaultService.createPrintJob(); //创建打印作业
             fis = new FileInputStream(file); //构造待打印的文件流
             DocAttributeSet das = new HashDocAttributeSet();
             das.add(new MediaPrintableArea(0, 0, 400, 300, MediaPrintableArea.MM));
             Doc doc = new SimpleDoc(fis, dof, das);
             job.print(doc, pras);
         } catch (Exception e) {
                    e.printStackTrace();
          } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public static void initStream(byte[] ArrayLetters,DocFlavor dof){
        //构建打印请求属性集
        HashPrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
        pras.add(OrientationRequested.PORTRAIT);
        pras.add(PrintQuality.HIGH);
        //定位默认的打印服务
        PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
        InputStream fis =null;
        try {
            DocPrintJob job =  defaultService.createPrintJob(); //创建打印作业
          //  fis = new FileInputStream(file); //构造待打印的文件流
            ByteArrayInputStream bais = new ByteArrayInputStream(ArrayLetters);
            DocAttributeSet das = new HashDocAttributeSet();
            das.add(new MediaPrintableArea(0, 0, 40, 30, MediaPrintableArea.MM));
         //   das.add(new MediaPrintableArea(1, 1, 4, 3, MediaPrintableArea.INCH));
            Doc doc = new SimpleDoc(bais, dof, das);
            job.print(doc, pras);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }




}
